package com.avasthi.cli.twitter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.Vector;

import com.aliasi.spell.JaroWinklerDistance;

import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class TwitterJsonWordPairGenerator {

	static Map<String, Set<String>> eqs = new HashMap<String, Set<String>>();

	static private boolean findDuplicateRecordAndAdd(String str1, String str2) {
		for (Entry<String, Set<String>> e : eqs.entrySet()) {
			if (e.getKey().equals(str1)) {
				e.getValue().add(str2);
				return true;
			} else if (e.getKey().equals(str2)) {
				e.getValue().add(str1);
				return true;
			} else {
				for (String v : e.getValue()) {
					if (v.equals(str1)) {
						e.getValue().add(str2);
						return true;
					}
					if (v.equals(str2)) {
						e.getValue().add(str1);
						return true;
					}
				}
			}
		}
		Set<String> set = new HashSet<String>();
		set.add(str2);
		eqs.put(str1, set);
		return false;
	}

	static private void printMap(PrintWriter writer) {
		for (Entry<String, Set<String>> e : eqs.entrySet()) {
			writer.print(e.getKey() + " ");

			for (String v : e.getValue()) {
				writer.print(v + " ");
			}
			writer.println("");
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		File inp = new File("/home/vavasthi/tweetscateg/hashtags.txt");
		System.out.println("Reading file :" + inp.getAbsolutePath());
		Vector<String> sv = new Vector<String>();
		try {
			FileReader fr = new FileReader(inp);
			BufferedReader reader = new BufferedReader(fr);
			String word;
			while ((word = reader.readLine()) != null) {

				if (word == null) {
					break;
				}
				sv.add(word);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] words = new String[sv.size()];
		words = sv.toArray(words);
		int k = 0;
		try {
			PrintWriter writer = new PrintWriter(
					"/home/vavasthi/tweetscateg/hashtags-pairs.txt", "UTF-8");

//			writer.printf("\n%18s  %18s  %5s  %5s\n", "Word1", "Word2", "Dist",
//					"Prox");

			JaroWinklerDistance jaroWinkler = JaroWinklerDistance.JARO_WINKLER_DISTANCE;
			for (int i = 0; i < words.length - 1; ++i) {
				for (int j = i + 1; j < words.length; ++j) {
					if (jaroWinkler.proximity(words[i], words[j]) >= 0.9) {
						findDuplicateRecordAndAdd(words[i], words[j]);
						/*
						 * writer.printf("%18s  %18s  %5.3f  %5.3f\n", words[i],
						 * words[j], jaroWinkler.distance(words[i], words[j]),
						 * jaroWinkler.proximity(words[i], words[j]));
						 */
					}
				}
			}
			printMap(writer);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}